/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.source.service.direction;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.source.entity.direction.EleSourceDirectionGroup;
import com.thinkgem.jeesite.modules.source.dao.direction.EleSourceDirectionGroupDao;
import com.thinkgem.jeesite.modules.source.entity.direction.EleSourceDirection;
import com.thinkgem.jeesite.modules.source.dao.direction.EleSourceDirectionDao;

/**
 * 方向资源管理Service
 * @author ZC
 * @version 2017-04-24
 */
@Service
@Transactional(readOnly = true)
public class EleSourceDirectionGroupService extends CrudService<EleSourceDirectionGroupDao, EleSourceDirectionGroup> {

	@Autowired
	private EleSourceDirectionDao eleSourceDirectionDao;
	
	public EleSourceDirectionGroup get(String id) {
		EleSourceDirectionGroup eleSourceDirectionGroup = super.get(id);
		eleSourceDirectionGroup.setEleSourceDirectionList(eleSourceDirectionDao.findList(new EleSourceDirection(eleSourceDirectionGroup)));
		return eleSourceDirectionGroup;
	}
	
	public List<EleSourceDirectionGroup> findList(EleSourceDirectionGroup eleSourceDirectionGroup) {
		return super.findList(eleSourceDirectionGroup);
	}
	
	public Page<EleSourceDirectionGroup> findPage(Page<EleSourceDirectionGroup> page, EleSourceDirectionGroup eleSourceDirectionGroup) {
		return super.findPage(page, eleSourceDirectionGroup);
	}
	
	@Transactional(readOnly = false)
	public void save(EleSourceDirectionGroup eleSourceDirectionGroup) {
		super.save(eleSourceDirectionGroup);
		for (EleSourceDirection eleSourceDirection : eleSourceDirectionGroup.getEleSourceDirectionList()){
			if (eleSourceDirection.getId() == null){
				continue;
			}
			if (EleSourceDirection.DEL_FLAG_NORMAL.equals(eleSourceDirection.getDelFlag())){
				if (StringUtils.isBlank(eleSourceDirection.getId())){
					eleSourceDirection.setGroup(eleSourceDirectionGroup);
					eleSourceDirection.preInsert();
					eleSourceDirectionDao.insert(eleSourceDirection);
				}else{
					eleSourceDirection.preUpdate();
					eleSourceDirectionDao.update(eleSourceDirection);
				}
			}else{
				eleSourceDirectionDao.delete(eleSourceDirection);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(EleSourceDirectionGroup eleSourceDirectionGroup) {
		super.delete(eleSourceDirectionGroup);
		eleSourceDirectionDao.delete(new EleSourceDirection(eleSourceDirectionGroup));
	}
	
}